/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.content.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.dao.category.CategoryDao;
import org.grayrabbit.cms.dao.content.LinkContentDao;
import org.grayrabbit.cms.dao.site.SiteDao;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.LinkContent;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.service.content.ContentService;
import org.grayrabbit.cms.service.content.LinkContentService;
import org.grayrabbit.cms.vo.content.ContentBean;
import org.grayrabbit.cms.vo.content.LinkContentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 14:40:11  
 */
@Service
@Transactional
public class LinkContentServiceImpl extends GenericServiceImpl implements LinkContentService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("linkContentDaoImpl")
	private LinkContentDao linkContentDao;
	@Autowired
	@Qualifier("categoryDaoImpl")
	private CategoryDao categoryDao;
	@Autowired
	@Qualifier("siteDaoImpl")
	private SiteDao siteDao;
	/**
	 * 此方法执行时为只读事务：
	 * @see ContentService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public LinkContent getLinkContent(final String linkContentId){
		LinkContent linkContent = linkContentDao.findById(linkContentId);
		linkContentDao.initialize(linkContent);
		linkContentDao.initialize(linkContent.getCategory());
		linkContentDao.initialize(linkContent.getContentAttachs());
		linkContentDao.initialize(linkContent.getContentTopic());
		linkContentDao.initialize(linkContent.getTopic());
		return linkContent;
	}
	
	/**
	 * @see ContentService#delete(String)
	 */
	@Transactional
	public void delete(final LinkContent linkContent) {
		linkContentDao.makeTransient(linkContent);
	}
	
	/**
	 * @see ContentService#save(OperLog)
	 */
	@Transactional
	public void save(final LinkContent linkContent, final Admin user,final String cflag) {
		if(linkContent!=null){
			if(StringUtils.isBlank(linkContent.getContentId())){
				if(StringUtils.isNotBlank(user.getLoginName())){
					linkContent.setCreateLoginName(user.getLoginName());
					linkContent.setUpdateLoginName(user.getLoginName());
				}
				if(StringUtils.isNotBlank(user.getRealName())){
					linkContent.setCreateRealName(user.getRealName());
					linkContent.setUpdateRealName(user.getRealName());
				}else{
					linkContent.setCreateRealName(user.getLoginName());
					linkContent.setUpdateRealName(user.getLoginName());
				}
				linkContent.setCreateTime(new Date());
				linkContent.setUpdateTime(new Date());
				linkContent.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_NEW);
				linkContent.setIsLocked(new Boolean(false));
			}else if(DictionaryStaticValues.CONTENT_STATUS_SUCCESS.equals(cflag)
					||DictionaryStaticValues.CONTENT_STATUS_FAIL.equals(cflag)){
				linkContent.setAuditLoginName(user.getLoginName());
				linkContent.setAuditRealName(user.getRealName());
				linkContent.setAuditTime(new Date());
				linkContent.setUpdateLoginName(user.getLoginName());
				linkContent.setUpdateRealName(user.getRealName());
				linkContent.setUpdateTime(new Date()); 
				if(linkContent.getPublishTime()==null){
					linkContent.setPublishTime(new Date());
				}
				linkContent.setContentStatus(cflag);
				linkContent.setIsLocked(new Boolean(false));
			}else if(DictionaryStaticValues.CONTENT_STATUS_PUBLISH.equals(cflag)){
				linkContent.setPublishLoginName(user.getLoginName());
				linkContent.setPublishRealName(user.getRealName());
				if(linkContent.getPublishTime()==null){
					linkContent.setPublishTime(new Date());
				}
				linkContent.setContentStatus(cflag);
				linkContent.setIsLocked(new Boolean(false));
			}else if(DictionaryStaticValues.CONTENT_STATUS_LOCK.equals(cflag)){
				/*锁定不记录修改信息*/
				linkContent.setIsLocked(new Boolean(true));
			}else{
				if(cflag.equals(DictionaryStaticValues.CONTENT_STATUS_NEW)
						||cflag.equals(DictionaryStaticValues.CONTENT_STATUS_PUBLISH)){
					if(linkContent.getTemporaryUser()!=null){
						linkContent.setUpdateRealName(linkContent.getTemporaryUser());
					}else{
						linkContent.setUpdateRealName(user.getRealName());
					}
				}else{ 
					linkContent.setUpdateRealName(user.getRealName());
					linkContent.setTemporaryUser(user.getRealName());
				}
				linkContent.setUpdateLoginName(user.getLoginName());
				linkContent.setUpdateRealName(user.getRealName());
				linkContent.setUpdateTime(new Date()); 
				linkContent.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_NEW);
				linkContent.setIsLocked(new Boolean(false));
			}
		}
		linkContentDao.makePersistent(linkContent);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see ContentService#getPage(ContentBean)
	 */
	@Transactional(readOnly=true)
	public Page<LinkContent> getPage(final LinkContentBean linkContentBean){
		Page<LinkContent> page =  new Page<LinkContent>();
		page.setTotal(linkContentDao.count(linkContentBean));
		page.setResult(linkContentDao.findByQc(linkContentDao.assembly(linkContentBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see ContentService#list(Page, ContentBean)
	 */
	public void list(Page<LinkContent> page, LinkContentBean linkContentBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(linkContentDao.count(linkContentBean));
		}
		page.setResult(linkContentDao.findByPage(page,linkContentBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see ContentService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			linkContentDao.makeTransient(linkContentDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				linkContentDao.clear();
		}
		
	}
	public List<LinkContent> findContent(String siteArea, String cateName,
			String catSimpleName,int max) {
		String categoryId = categoryDao.findCatIdByCatName(cateName, catSimpleName, null, siteArea);
		return linkContentDao.findContent(categoryId,  max);
	}
	
	public String loadSiteDomainAndFLinks(String siteArea, String cateName,
			String catSimpleName,int max) {
		try {			
			String categoryId = categoryDao.findCatIdByCatName(cateName, catSimpleName, null, siteArea);
			if(StringUtils.isBlank(categoryId)){
				throw new NullPointerException("当前站点没有\"友情链接\"栏目！");
			}
			List<LinkContent> list = linkContentDao.findContent(categoryId,  max);
			String domain = siteDao.findSiteDomain(siteArea);
			
			StringBuffer sb = new StringBuffer();
			sb.append("{\"link\":\"");
			
			int n = list.size();
			sb.append("<span class='b'>友情链接：</span>");		
			for(LinkContent l: list){
				sb.append("<a href='")
				  .append(l.getLinkUrl())
				  .append("' target='_blank' >")
				  .append(l.getContentTitle())
				  .append("</a>");
				n--;
				if(n>0){
					sb.append("&#160;|&#160;");
				}
			}
			
			sb.append("<span class='l'></span>");
			sb.append("<span class='r'></span>");
			
			sb.append("\",\"domain\":\"")
				.append(domain)
				.append("\",\"msg\":\"加载成功\",\"flag\":true}");
		
			list.clear();
			siteDao.clear();
			return sb.toString();
		} catch (Exception e) {
			return "{\"flag\":false,\"msg\":\"友情链接加载异常\"}";
		}		
	}
	
	
}